// pages/index/home.js
const app = getApp()
let ServiceUrl = app.globalData.ServiceUrl;
Page({
  data: {
    user_id:'',
    listAssistance:[],//初始化数据，助医服务最新订单
    listAccompany: [],//陪诊服务最新订单 
    //上拉加载
//    page:1,//页数
//    isLastPage:false,//是否最后一页数据
    active: 0//tabbar方法
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log(options.user_id)
    that.setData({
      user_id: wx.getStorageSync('uid') ? wx.getStorageSync('uid') : options.user_id,
    })
  },
  /**
   * onTabbarChange tabbar功能
   * @param {*} event 
   */
  onTabbarChange(event) {
    this.setData({ active: event.detail });
    //console.log(event.detail)
    let user_id = this.data.user_id
    if(event.detail == 1){
      wx.redirectTo({
        url: '../index/index?user_id=' + user_id,
      })
    }else if(event.detail == 0){
      wx.redirectTo({
        url: '../index/home?user_id=' + user_id,
      })
    }
  },
  //下拉加载
  onReachBottom:function (){
    //console.log('下拉了')
  },
  //助医服务加载数据方法
  requestAssistance(){
    let that = this;
    let user_id = that.data.user_id;
//    console.log(user_id)
    //user_id不为空
    if( user_id != ''){
      console.log('user_id='+user_id)
      // wx.showLoading({title:"加载中..."});
      // setTimeout(res=>{
      //   wx.hideLoading();
      // },1500)
      wx.request({
        url: ServiceUrl + 'getMainList',
        data:{
          user_id: user_id,
//          page:that.data.page
        },
        success:(res)=>{
          console.log(res)
          if(res.data.state == 200){
            //wx.hideLoading();
//            console.log(res.data.data)
            that.setData({
              listAssistance: res.data.data
            })
          }
        },
        fail:function(res){
          wx.showModal({
            title:"提示",
            content:"发送请求失败",
            showCancel: false,
            confirmColor: 'skyblue'
          })
        }
      })
    }
  },
  //助医服务加载数据方法
  requestAccompany(){
    let that = this;
    let user_id = that.data.user_id;
    //user_id不为空
    if( user_id != ''){
//      console.log('user_id=' + user_id)
      wx.showLoading({title:"加载中..."});
      setTimeout(res=>{
        wx.hideLoading();
      },1500)
      wx.request({
        url: ServiceUrl + 'getNewAccompany',
        data:{
          user_id: user_id,
//          page:that.data.page
        },
        success:(res)=>{
          console.log(res)
          if(res.data.state == 200){
            wx.hideLoading();
            console.log(res.data.data)
            that.setData({
              listAccompany: res.data.data
            }) 
          }
        },
        fail:function(res){
          wx.showModal({
            title:"提示",
            content:"发送请求失败",
            showCancel: false,
            confirmColor: 'skyblue'
          })
        }
      })
    }
  },
  //陪伴门诊跳转链接
  menzhenUrl: function(){
    let user_id = this.data.user_id;
    console.log(user_id);
    this.setData({state:1})
    wx.navigateTo({
      url: '../information/accompany?typeid=1&user_id='+user_id,
    })
  },
  //陪伴血透跳转链接
  xuetouUrl: function(){
    let user_id = this.data.user_id;
    console.log(user_id);
    this.setData({state:1})
    wx.navigateTo({
      url: '../information/accompany?typeid=2&user_id='+user_id,
    })
  },
  //代配药跳转链接
  dosageUrl: function(){
    let user_id = this.data.user_id;
    console.log(user_id);
    this.setData({state:1})
    wx.navigateTo({
      url: '../information/accompany?typeid=3&user_id='+user_id,
    })
  },
  //非门诊就医跳转链接
  jiuyiUrl: function(){
      let user_id = this.data.user_id;
      console.log(user_id);
      this.setData({state:1})
      wx.navigateTo({
        url: '../information/assistance?typeid=1&user_id='+user_id,
      })
  },
  //住院协调跳转链接
  zhuyuanUrl: function(){
      let user_id = this.data.user_id;
      this.setData({state:1})
      wx.navigateTo({
        url: '../information/assistance?typeid=2&user_id='+user_id,
      })
  },
  //点名手术跳转链接
  shoushuUrl: function(){
      let user_id = this.data.user_id;
      this.setData({state:1})
      wx.navigateTo({
        url: '../information/assistance?typeid=3&user_id='+user_id,
      })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

   /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.setData({
      listAccompany:[],
      listAccompany:[],
      page: 1,
      isLastPage:false,//是否最后一页数据
      active: 0//tabbar方法
    })
    that.requestAssistance();//助医服务流加载数据渲染
    that.requestAccompany();//陪诊服务流加载数据渲染
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this
    that.requestAssistance();//助医服务流加载数据渲染
    that.requestAccompany();//陪诊服务流加载数据渲染
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let user_id = wx.getStorageSync('uid')
    wx.removeStorageSync('uid');
    return {
      path: '/pages/index/home',
      success:function(){
        wx.setStorageSync('uid', user_id)
      }
    }
  }
})